package com.lm.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lm.controller.UploadController;
import com.lm.mapper.DocterMapper;
import com.lm.mapper.OrgMapper;
import com.lm.mapper.TeamManageMapper;
import com.lm.pojo.Docter;
import com.lm.pojo.Organization;
import com.lm.pojo.SearchOrganization;
import com.lm.pojo.TeamManage;
import com.lm.service.OrgService;
import com.lm.util.AliYunImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @version 1.0
 * @dscription:TODO
 * @author32477
 * @date 2025/9/20 23:24
 */
@Service
public class OrgServiceImpl implements OrgService {
    @Autowired
    OrgMapper orgMapper;

    @Autowired
    DocterMapper docterMapper;

    @Autowired
    UploadController aliYunImage;
    @Autowired
    TeamManageMapper teamManageMapper;


    @Override
    public List<TeamManage> findTeamList(Integer orgId) {
        QueryChainWrapper<TeamManage> query = new QueryChainWrapper<>(teamManageMapper);
        query.eq("docter_team_org_id", orgId);
        return query.list();
    }

    @Override
    public List<Organization> findIdAndName() {
        return orgMapper.findIdAndName();
    }
    @Override
    public IPage<Organization> findList(SearchOrganization org) {
        // 开启分页 获取起始页
        IPage<Organization> page = new Page<>(org.getPageNo(),org.getPageSize());
        IPage<Organization> pageInfo = orgMapper.findList(page, org.getOrganizationName());
        return pageInfo;
    }
    @Override
    public void addOrg(Organization org) {
        orgMapper.insert(org);
    }
    @Override
    @Transactional
    public void deleteOrg(Integer orgId) {
        //查找图片
        Organization org = orgMapper.selectById(orgId);
        if (org.getOrgAvator() != null) {
            //删除图片
            aliYunImage.deleteImg(org.getOrgAvator());
        }

        orgMapper.deleteById(orgId);
    }
    @Override
    public void updateOrg(Organization org) {
        orgMapper.updateById(org);
    }
    @Override
    public Integer findLastNumber() {
        return orgMapper.findLastNumber();
    }
    @Override
    public List<Docter> findDocterList() {
        return docterMapper.selectList(null);
    }

    @Override
    public void changeState(Integer orgId, Integer orgState) {
        orgMapper.changeState(orgId,orgState);
    }
}
